package com.example.test;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * @author zhuyaotong
 * @date 2021/05/29
 */
public class ActivitiBusinessDemo {

    /**
     * 添加业务 key 到 Activiti 的表
     */
    @Test
    public void addBusinessKey() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("myEvection", "1001");
        System.out.println("businessKey==" + instance.getBusinessKey());
    }

    /**
     * 全部流程实例的 挂起 和 激活
     * suspend 暂停
     */
    @Test
    public void suspendAllProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection").singleResult();

        boolean suspended = processDefinition.isSuspended();
        String definitionId = processDefinition.getId();

        if (suspended) {
            repositoryService.activateProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义ID：" + definitionId + "，已激活");
        } else {
            repositoryService.suspendProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义ID：" + definitionId + "，已挂起");
        }
    }

    /**
     * 挂起、激活单个流程实例
     */
    @Test
    public void suspendSingleProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("17501")
                .singleResult();

        boolean suspended = instance.isSuspended();
        String instanceId = instance.getId();
        if (suspended) {
            runtimeService.activateProcessInstanceById(instanceId);
            System.out.println("流程实例ID：" + instanceId + "，已激活");
        } else {
            runtimeService.suspendProcessInstanceById(instanceId);
            System.out.println("流程实例ID：" + instanceId + "，已暂停");
        }
    }

    /**
     * 完成个人任务
     */
    @Test
    public void completeTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processInstanceId("17501")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例ID：" + task.getProcessInstanceId());
        System.out.println("流程任务ID：" + task.getId());
        System.out.println("负责人：" + task.getAssignee());
        System.out.println("任务名称：" + task.getName());

        taskService.complete(task.getId());
    }

}
